//
//  XQIdentityAccureView.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/23.
//

#import "XQIdentityAccureView.h"
#import "XQIdentityAccureCell.h"

@interface XQIdentityAccureView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,copy) NSArray *dataSource;
@property (nonatomic,copy) NSString *title;
@property (nonatomic,strong) UIView *topView;
@property (nonatomic,strong) UILabel *topTitleLb;
@property (nonatomic,strong) UIButton *topButton;
@property (nonatomic,strong) UITableView *tableView;
@end

@implementation XQIdentityAccureView

- (instancetype)initWithTitle:(NSString *)title dataSource:(NSArray *)array {
    self = [super init];
    if (self) {
        _title = title;
        _dataSource = array;
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self addSubview:self.topView];
    [self.topView addSubview:self.topTitleLb];
    [self.topView addSubview:self.topButton];
    [self addSubview:self.tableView];
}

- (void)makeConstraints {
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(52);
    }];
    [self.topTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@16);
        make.left.equalTo(@20);
        make.height.mas_equalTo(22);
    }];
    [self.topButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.topTitleLb);
        make.right.equalTo(@-20);
        make.height.width.equalTo(@20);
    }];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_top).offset(52);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.height.mas_equalTo(0);
        make.bottom.equalTo(@0);
    }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XQIdentityAccureCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQIdentityAccureCell" forIndexPath:indexPath];
    [cell setIsLast:indexPath.row == self.dataSource.count - 1];
    [cell setTitle:self.dataSource[indexPath.row]];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.row == self.dataSource.count - 1 ? 21 : 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.didSelectHandle) {
        self.didSelectHandle(self.dataSource[indexPath.row]);
    }
}

#pragma mark - setter

- (void)setArrowShow:(BOOL)arrowShow {
    _arrowShow = arrowShow;
    NSString *imageName = arrowShow ? [NSString stringWithFormat:@"arrow_down"] : [NSString stringWithFormat:@"arrow_right"];
    [self.topButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    
    [self.topView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(arrowShow ? 90 : 52);
    }];
    
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        if (arrowShow) {
            make.height.mas_equalTo(40 + 21 * self.dataSource.count + 29 * (self.dataSource.count - 1));
        }else {
            make.height.mas_equalTo(0);
        }
    }];
}

#pragma mark - private

- (void)buttonClick {
    self.arrowShow = self.arrowShow ? NO : YES;
    if (self.arrowHandle) {
        self.arrowHandle(self.arrowShow);
    }
}

#pragma mark - lazy

- (UIView *)topView {
    if (!_topView) {
        _topView = [[UIView alloc] init];
        _topView.layer.cornerRadius = 15;
        _topView.backgroundColor = kBlueTextColor;
    }
    return _topView;
}

- (UILabel *)topTitleLb {
    if (!_topTitleLb) {
        _topTitleLb = [[UILabel alloc] init];
        _topTitleLb.text = self.title;
        _topTitleLb.textColor = [UIColor whiteColor];
        _topTitleLb.font = [UIFont boldSystemFontOfSize:16];
    }
    return _topTitleLb;
}

- (UIButton *)topButton {
    if (!_topButton) {
        _topButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_topButton addTarget:self action:@selector(buttonClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _topButton;
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.layer.cornerRadius = 15;
        [_tableView setScrollEnabled:NO];
        [_tableView setContentInset:UIEdgeInsetsMake(20, 0, 20, 0)];
        [_tableView registerClass:[XQIdentityAccureCell class] forCellReuseIdentifier:@"XQIdentityAccureCell"];
    }
    return _tableView;
}


@end
